package com.example.mynews;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import q.rorbin.badgeview.QBadgeView;

import android.os.Bundle;
import android.util.LruCache;
import android.view.Menu;
import android.view.MenuItem;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.example.base.activity.MvvmActivity;
import com.example.base.viewmodel.MvvmBaseViewModel;
import com.example.mynews.databinding.ActivityMainBinding;
import com.example.mynews.fragments.AccountFragment;
import com.example.mynews.fragments.CategoryFragment;
import com.example.mynews.fragments.ServiceFragment;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;


public class MainActivity extends MvvmActivity<ActivityMainBinding, MvvmBaseViewModel> {
    private Fragment mHomeFragment;
    private CategoryFragment mCategoryFragment = new CategoryFragment();
    private ServiceFragment mServiceFragment = new ServiceFragment();
    private AccountFragment mAccountFragment = new AccountFragment();
    //当前显示的Fragment
    private Fragment nowFragment;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected MvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CCResult ccResult = CC.obtainBuilder("News").setActionName("getHeadlineNewsFragment").build().call();
        mHomeFragment = (Fragment) ccResult.getDataMap().get("fragment");
        nowFragment = mHomeFragment;
        //set Toolbar
        setSupportActionBar(viewDataBinding.toolbar);
        //左上角的图标显示
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        //左上角的图标加一个返回图标
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle(getString(R.string.menu_home));
        //显示Fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(viewDataBinding.container.getId(), mHomeFragment);
        transaction.commit();
        viewDataBinding.bottomView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.menu_home) {
                switchFragment(mHomeFragment);
            } else if (itemId == R.id.menu_categories) {
                switchFragment(mCategoryFragment);
            } else if (itemId == R.id.menu_services) {
                switchFragment(mServiceFragment);
            } else if (itemId == R.id.menu_account) {
                switchFragment(mAccountFragment);
            }
            //设置标题
            if (getSupportActionBar() != null) {
                getSupportActionBar().setTitle(item.getTitle());
            }
            return true;
        });
        //模拟显示红点
        showBadgeView(3, 1);
    }

    /**
     * 切换显示的Fragment
     *
     * @param tagFragment 需要显示的Fragment
     */
    private void switchFragment(Fragment tagFragment) {
        //每个事务对象只能被commit一次,所以这里每次都要getSupportFragmentManager().beginTransaction()
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (nowFragment != tagFragment) {
            if (!tagFragment.isAdded()) {
                if (nowFragment != null) {
                    transaction.hide(nowFragment);
                }
                if (tagFragment != null) {
                    transaction.add(viewDataBinding.container.getId(), tagFragment).commit();
                }
            } else {
                if (nowFragment != null) {
                    transaction.hide(nowFragment);
                }
                if (tagFragment != null) {
                    transaction.show(tagFragment).commit();
                }
            }
            nowFragment = tagFragment;
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(com.example.base.R.menu.toolbar_menu, menu);
        return true;
    }

    /**
     * BottomNavigationView 显示角标
     *
     * @param viewIndex  tab索引
     * @param showNumber 角标显示的数字，小于等于0将不显示
     */
    private void showBadgeView(int viewIndex, int showNumber) {
        //从BottomNavigationView中获得BottomNavigationMenuView
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) viewDataBinding.bottomView.getChildAt(0);
        if (3 < menuView.getChildCount()) {
            //从BottomNavigationMenuView中获得BottomNavigationItemView
            BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(3);
//            View icon = itemView.findViewById(com.google.android.material.R.id.icon);
//            int iconWidth = icon.getWidth();
            int tabWidth = itemView.getWidth() / 2;
//            int spaceWidth = tabWidth - iconWidth;
            //新建并显示QBadgeView
            new QBadgeView(this).bindTarget(itemView).setGravityOffset(tabWidth + 50, 13, false).setBadgeNumber(showNumber);
        }
    }


}